﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text;

public partial class CustomerManagement_AddDiscountAlterationManagement : System.Web.UI.Page
{
    private DataSet DstDetails
    {
        get
        {
            DataSet dstDetails = new BLL.DISCADJUSTDETAIL().CreateTempDataTable();
            if (ViewState["DstDetails"] != null)
            {
                dstDetails = (DataSet)ViewState["DstDetails"];
            }
            return dstDetails;
        }
        set
        {
            ViewState["DstDetails"] = value;
        }
    }

    private List<Model.V_CARSET> Cards
    {
        get
        {
            List<Model.V_CARSET> cards = null;
            if (ViewState["Cards"] != null)
            {
                cards = (List<Model.V_CARSET>)ViewState["Cards"];
            }
            return cards;
        }
        set
        {
            ViewState["Cards"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("300400101");
        if (!IsPostBack)
        {
            btnCancel.Attributes.Add("onclick", "if(!confirm('要取消新建结算单并返回上一页吗?'))return false;");
        }
        Session["FCode"] = "300400101";
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!Check.CheckPermission("300400101", "00017"))
        {
            Share.Alert(this.Page, "无权操作"); return;
        }
        try
        {
            SetValueOfDataTable(this.DstDetails);
            if (this.DstDetails.Tables[0].Rows.Count == 0)
            {
                throw new Exception("请添加会员！");

            }
            string newId = "";

            if (new BLL.DISCADJUST().InsertNew(this.DstDetails, Share.UserValue(0), out newId))
            {
                Share.InsertLog("300400101", "00017");
                Response.Write("<script>alert('新增成功！');window.location.href='DiscountAlterationManagementDetail.aspx?code=" + newId + "'</script>");
            }
            else
            {
                Share.Alert(this.Page, "新增未成功，请重新添加！");
            }

        }
        catch (Exception ex)
        {
            Share.Alert(this.Page, ex.Message);
        }

    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("LineOfCreditManagement.aspx");
    }
    protected void dlistDetail_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "del")
        {
            if (this.DstDetails != null)
            {
                this.DstDetails.Tables[0].Rows.RemoveAt(e.Item.ItemIndex);
                dlistDetail.DataSource = this.DstDetails;
                dlistDetail.DataBind();
            }
        }
    }
    protected void btnAddDetail_Click(object sender, EventArgs e)
    {
        try
        {
            string customerCode = hidUserCode.Value;
            SetValueOfDataTable(this.DstDetails);
            if (customerCode != "")
            {
                string[] customersArray = customerCode.Split(',');
                if (customersArray.Length > 0)
                {
                    string[] customer;
                    for (int i = 0; i < customersArray.Length; i++)
                    {
                        customer = customersArray[i].Split('|');
                        if (customer.Length == 3)
                        {
                            

                            Model.V_VIPSET customerInfo = new BLL.V_VIPSET().GetModel(customer[0]);
                            if (customerInfo == null)
                            {
                                throw new Exception("无法获取会员原始折扣信息！");
                            }
                            if (this.Cards == null)
                            {
                                this.Cards = new BLL.V_CARSET().GetAllList();
                            }
                            this.DstDetails = new BLL.DISCADJUSTDETAIL().AddDataRowToDataTable(this.DstDetails, Convert.ToString(this.DstDetails.Tables[0].Rows.Count + 1), customer[0], customer[1],customerInfo.KLDM,customerInfo.ZK,this.Cards[1].DM,this.Cards[1].ZK);
                            
                        }
                    }
                    dlistDetail.DataSource = this.DstDetails;
                    dlistDetail.DataBind();
                }
            }
            else
            {
                return;//Share.Alert(this.Page, "请选择会员！");
            }
            hidUserCode.Value = "";
        }
        catch (Exception ex)
        {
            Share.Alert(this.Page, ex.Message);
        }
    }
    private void SetValueOfDataTable(DataSet dst)
    {
        Label lblDetailId;
        Label lblCustomerCode;
        Label lblCustomerName;
        DropDownList dropOriginalLevel;
        Label lblOriginalDiscount;
        DropDownList dropRegulateLevel;
        TextBox txtRegulateDiscount;
        TextBox txtNote;

        for (int i = 0; i < dlistDetail.Items.Count; i++)
        {
            lblDetailId = (Label)dlistDetail.Items[i].FindControl("lblDetailId");
            lblCustomerCode = (Label)dlistDetail.Items[i].FindControl("lblCustomerCode");
            lblCustomerName = (Label)dlistDetail.Items[i].FindControl("lblCustomerName");
            dropOriginalLevel = (DropDownList)dlistDetail.Items[i].FindControl("dropOriginalLevel");
            lblOriginalDiscount = (Label)dlistDetail.Items[i].FindControl("lblOriginalDiscount");
            dropRegulateLevel = (DropDownList)dlistDetail.Items[i].FindControl("dropRegulateLevel");
            txtRegulateDiscount = (TextBox)dlistDetail.Items[i].FindControl("txtRegulateDiscount");
            txtNote = (TextBox)dlistDetail.Items[i].FindControl("txtNote");

            dst.Tables[0].Rows[i]["DAD_DETAILCODE"] = lblDetailId.Text;
            dst.Tables[0].Rows[i]["DAD_CUSTOMERCODE"] = lblCustomerCode.Text;
            dst.Tables[0].Rows[i]["GKMC"] = lblCustomerName.Text;
            dst.Tables[0].Rows[i]["OriginalLevel"] = dropOriginalLevel.SelectedValue;
            dst.Tables[0].Rows[i]["OriginalDiscount"] = lblOriginalDiscount.Text;
            dst.Tables[0].Rows[i]["RegulateLevel"] = dropRegulateLevel.SelectedValue;
            dst.Tables[0].Rows[i]["RegulateDiscount"] = Convert.ToDecimal(txtRegulateDiscount.Text.Trim());
            dst.Tables[0].Rows[i]["DAD_NOTE"] = txtNote.Text.Trim();
        }

    }

    private void BindDropCardLevel(DropDownList drop)
    {
        if (this.Cards == null)
        {
            this.Cards = new BLL.V_CARSET().GetAllList();
        }

        drop.DataSource = this.Cards;
        drop.DataTextField = "MC";
        drop.DataValueField = "DM";
        drop.DataBind();
    }

    protected void dlistDetail_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            DropDownList dropOriginalLevel = (DropDownList)e.Item.FindControl("dropOriginalLevel");
            DropDownList dropRegulateLevel = (DropDownList)e.Item.FindControl("dropRegulateLevel");
            if (dropOriginalLevel != null)
            {
                BindDropCardLevel(dropOriginalLevel);
                for (int i = 0; i < dropOriginalLevel.Items.Count; i++)
                {
                    for (int j = 0; j < this.DstDetails.Tables[0].Rows.Count; j++)
                    {
                        if (dropOriginalLevel.Items[i].Value == this.DstDetails.Tables[0].Rows[j]["OriginalLevel"].ToString())
                        {
                            dropOriginalLevel.SelectedIndex = i;
                            break;
                        }
                    }
                    
                }
            }

            if (dropRegulateLevel != null)
            {
                BindDropCardLevel(dropRegulateLevel);
                for (int i = 0; i < dropRegulateLevel.Items.Count; i++)
                {
                    for (int j = 0; j < this.DstDetails.Tables[0].Rows.Count;j++ )
                    {
                        if (dropRegulateLevel.Items[i].Value == this.DstDetails.Tables[0].Rows[j]["RegulateLevel"].ToString())
                        {
                            dropRegulateLevel.SelectedIndex = i;
                            break;
                        }
                    }
                    
                }
            }
        }
    }

    protected void DataList2_DDLCHANGE_Click(object sender, EventArgs e)
    {
        DropDownList dropRegulateLevel = (DropDownList)sender;
        TextBox txtRegulateDiscount = (TextBox)dropRegulateLevel.Parent.FindControl("txtRegulateDiscount");
        foreach (Model.V_CARSET temp in this.Cards)
        {
            if (dropRegulateLevel.SelectedValue == temp.DM)
            {
                txtRegulateDiscount.Text = String.Format("{0:0.00}",temp.ZK);
                break;
            }
        }
    }

}
